package com.mxn.soul.slidingcard;

import com.mxn.soul.slidingcard.slice.MainAbilitySlice;
import com.mxn.soul.slidingcard_core.ContainerView;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class MainAbility extends Ability implements  ContainerView.onFlingListener{
    private String[] titles=new String[]{"当红小花旦越来越惊艳了","早秋长袖连衣裙刮起了唯美浪漫风","高品质裙装美照让你一次看过瘾",
            "蕾丝防晒衫棒球服外套","纯色开衫薄款外套防嗮衫"};
    private int[] images=new int[]{ResourceTable.Media_img1,ResourceTable.Media_img2,ResourceTable.Media_img3,ResourceTable.Media_img4,ResourceTable.Media_img5};
    private List<PhotoContent> al=new ArrayList<>();

    private ContainerView containerView;

    private BaseAdapter mProvider;
    private int mCount=0;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        super.setMainRoute(MainAbilitySlice.class.getName());
        super.setUIContent(ResourceTable.Layout_ability_main);
        containerView = (ContainerView) findComponentById(ResourceTable.Id_frame);
        for(int i=0;i<titles.length;i++){
            al.add(new PhotoContent(titles[i],images[i]));
        }
        mProvider = new BaseAdapter(this);
        mProvider.setData(al);
        containerView.setItemProvider(mProvider);
        containerView.setFlingListener(this);
        containerView.setOnItemClickListener(new ContainerView.OnItemClickListener() {
            @Override
            public void onItemClicked(int itemPosition, Object dataObject) {
                makeToast(al.get(itemPosition).getTitle());
            }
        });
    }

    @Override
    public void removeFirstObjectInAdapter() {
        al.remove(0);
        mProvider.notifyDataSetItemRemoved(0);
    }

    @Override
    public void onAdapterAboutToEmpty(int itemsInAdapter) {
        al.add(new PhotoContent(titles[mCount],images[mCount]));
        mProvider.notifyDataSetItemInserted(al.size() - 1);
        mCount++;
        if(mCount==titles.length){
            mCount=0;
        }
    }

    @Override
    public void onScroll(float scrollProgressPercent) {
        Component component = containerView.getSelectedComponent();
        float a= (float) 0.3;
        if(scrollProgressPercent>0){
            component.setAlpha(1-scrollProgressPercent+a);
        }
        else {
            component.setAlpha(1-(-scrollProgressPercent)+a);
        }
    }
    private void makeToast(String text) {
        ToastDialog toast = new ToastDialog(this);
        toast.setText(text);
        // 显示Toast会导致快速滑动删除卡顿
         toast.show();
    }
}
